package com.gopay.bis.citic.fund.operation;

import java.security.KeyPair;

import org.apache.log4j.Logger;

import org.apache.xml.security.exceptions.Base64DecodingException;
import org.apache.xml.security.utils.Base64;

/**
 * RSA加密适配器
 * @RsaAdapter.java
 * @author dong-wang5@hnari.com
 * @2016年7月20日 上午10:02:56  www.gopay.com.cn Inc.All rights reserved.
 */
public class RsaAdapter implements AlgorithmInterface 
{
	private Logger logger = Logger.getLogger(RsaAdapter.class);
    /** 
     * @param key
     * @param txt
     * @return
     */
    public String sign(String key, String txt) 
    {
		try {
			byte[] signedArray = RSA.sign(Base64.decode(key), txt.getBytes());
			 return Base64.encode(signedArray);
		} catch (Base64DecodingException e) {
			logger.fatal(e);
			return null;
		}
    }

    /** 
     * @param key
     * @param txt
     * @param signed
     * @return
     */
    public boolean verify(String key, String txt, String signed){
        try {
			return RSA.verify(Base64.decode(key), txt.getBytes(), Base64.decode(signed));
		} catch (Base64DecodingException e) {
			logger.fatal(e);
			return false;
		}
    }

    /** 
     * @param key
     * @param ciphertext
     * @return
     */
    public String decrypt(String key, String ciphertext) 
    {
        throw new UnsupportedOperationException();
    }

    /** 
     * @param key
     * @param plaintext
     * @return
     */
    public String encrypt(String key, String plaintext) 
    {
        throw new UnsupportedOperationException();
    }

    /** 
     * @param keySize
     * @return
     */
    public String generateKey(int keySize) 
    {
        throw new UnsupportedOperationException();
    }

    /** 
     * @param keySize
     * @param seed
     * @return
     */
    public KeyPair generateKeyPair(int keySize, String seed) 
    {
        return RSA.generateKeyPair(keySize, seed.getBytes());
    }
}
